// pages/search/search.js
import request from '../../utils/request'
let isSend = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    placeholderContent: '',//搜索框默认内容
    hotList: [],//热搜榜数据
    searchContent: '',//输入的搜索内容
    searchList: [],//模糊匹配结果
    historyList:[],//搜索历史记录
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //获取初始数据
    this.getInitData()
    //获取历史记录
    this.getSearchHistory()
  },
  //获取初始数据
  async getInitData() {
    //获取搜索框默认内容
    let placeholderData = await request('/search/default');
    //获取热搜榜数据
    let hotData = await request('/search/hot/detail');
    this.setData({
      placeholderContent: placeholderData.data.showKeyword,
      hotList: hotData.data
    })
  },
  //获取历史记录
  getSearchHistory(){
    let historyList = wx.getStorageSync('searchHistory')
    if(historyList){
      this.setData({
        historyList
      })
    }
  },
  //搜索框内容改变的回调
  handleInputChange() {
    //函数节流
    if(isSend){
      return
    }
    isSend = true;
    this.getSearchList()
    setTimeout(() => {
      isSend = false
    }, 30);
  },
  //获取搜索建议数据
  async getSearchList(){
    if(!this.data.searchContent){
      return
    }
    //发请求获取数据
    let searchListData = await request('/search/suggest', { keywords: this.data.searchContent.trim(), type: 'mobile' })
    this.setData({
      searchList: searchListData.result.allMatch
    })
  },
  //点击搜索触发的回调，添加历史记录
  handleConfirm(){
    //添加历史记录
    let {searchContent,historyList,placeholderContent} = this.data
    if(searchContent && (historyList.indexOf(searchContent)===-1)){
      historyList.unshift(searchContent)
    }else if(historyList.indexOf(placeholderContent) === -1){
      historyList.unshift(placeholderContent)
    }
    this.setData({
      historyList
    })
    wx.setStorageSync('searchHistory', historyList)
    //跳转至搜索结果页
    wx.navigateTo({
      url: '/pages/searchResult/searchResult?keywords='+searchContent,
    })
  },
  //跳转至搜索结果页
  toSearchResult(event){
    let searchContent = event.currentTarget.dataset.keywords
    console.log(searchContent)
    wx.navigateTo({
      url: '/pages/searchResult/searchResult?keywords='+searchContent,
    })
  },
  //点击清空搜索内容
  clearSearchContent(){
    this.setData({
      searchContent:'',
      searchList:[]
    })
  },
  //点击取消搜索
  cancelSearch(){
    this.setData({
      searchList:[],
    })
  },
  //点击清空历史记录
  clearHistoryList(){
    wx.showModal({
      content:'确认删除历史记录吗?',
      success:(res)=>{
        if(res.confirm){
          this.setData({
            historyList:[]
          })
          wx.removeStorageSync('searchHistory')
        }
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})